package com.joker.web.controller.orange;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.joker.common.core.controller.BaseController;
import com.joker.common.core.domain.PageQuery;
import com.joker.common.core.domain.R;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.helper.LoginHelper;
import com.joker.orange.domain.BuyOrder;
import com.joker.orange.domain.CollectionInfo;
import com.joker.orange.domain.PlayRecord;
import com.joker.orange.domain.ShortVideoUser;
import com.joker.orange.domain.bo.*;
import com.joker.orange.domain.vo.*;
import com.joker.orange.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 网格视频相关
 *
 * @author Joker
 * @date 2024/6/10 09:07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/grid/video")
public class AppGridVideoController extends BaseController {

    private final IVideoInfoService videoInfoService;
    private final PublicService publicService;
    private final IUserInfoService userInfoService;
    private final ICollectionService collectionService;
    private final ICarouselImagesService carouselImagesService;
    private final IPlayRecordService playRecordService;
    private final IShortVideoUserService shortVideoUserService;
    private final IAdvertisingSpaceService advertisingSpaceService;
    private final IUserLikeService userLikeService;
    private final IBuyOrderService buyOrderService;

    /**
     * 网格首页
     */
    @PostMapping("/selectGridIndexVideoList")
    public R<Map<String,Object>> selectGridIndexVideoList(){
        Map<String,Object> result = new HashMap<>();
        // 轮播图列表
        CarouselImagesBo imagesBo = new CarouselImagesBo();
        imagesBo.setCarouselType("1");
        imagesBo.setIsDisplay("0");
        List<CarouselImagesVo> imagesList = carouselImagesService.queryList(imagesBo);
        result.put("imagesList", imagesList);
        VideoInfoBo videoInfoBo = new VideoInfoBo();
        videoInfoBo.setVideoType("0");
        List<VideoInfoVo> videoInfoList = videoInfoService.queryList(videoInfoBo);
        // 正在热映
        result.put("hotVideoList", videoInfoService.getRandomVideos(videoInfoList, 6));
        // 网格专属
        result.put("videoList", videoInfoService.getRandomVideos(videoInfoList, 6));
        // 底部广告
        AdvertisingSpaceBo advertisingSpaceBo = new AdvertisingSpaceBo();
        advertisingSpaceBo.setAdvertisingType("5");
        advertisingSpaceBo.setIsDisplay("0");
        List<AdvertisingSpaceVo> advertisingList = advertisingSpaceService.queryList(advertisingSpaceBo);
        result.put("advertisingList", advertisingList);
        return R.ok(result);
    }


    /**
     * 搜索，分类，查看更多网格
     * categoryId 分类ID
     * videoName 视频名称
     * pageNum 当前页数
     * pageSize 分页大小
     */
    @PostMapping("/selectVideoList")
    public TableDataInfo<VideoInfoVo> selectVideoList(@RequestBody JSONObject jsonObject) {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(jsonObject.getInt("pageNum"));
        pageQuery.setPageSize(jsonObject.getInt("pageSize"));
        VideoInfoBo videoInfoBo = new VideoInfoBo();
        videoInfoBo.setVideoType("0");
        videoInfoBo.setCategoryId(jsonObject.getLong("categoryId"));
        videoInfoBo.setVideoName(jsonObject.getStr("videoName"));
        return videoInfoService.queryPageList(videoInfoBo, pageQuery);
    }

    /**
     * 视频详情
     * videoId 视频ID
     */
    @PostMapping("/selectVideoById")
    public R<Map<String, Object>> selectVideoById(@RequestBody JSONObject jsonObject) {
        if (ObjectUtil.isNull(jsonObject.get("videoId"))) {
            return R.fail("视频ID不能为空");
        }
        VideoInfoBo bo = new VideoInfoBo();
        bo.setId(jsonObject.getLong("videoId"));
        UserInfoVo userInfo = userInfoService.queryById(LoginHelper.getUserId());
        Map<String, Object> result = new HashMap<>();
        VideoInfoVo videoInfoVo = videoInfoService.queryById(bo.getId());
        videoInfoVo.setGridMemberExpireTime(userInfo.getGridMemberExpireTime());
        // 判断是否点赞和收藏
        CollectionInfo collectionInfo = new CollectionInfo();
        collectionInfo.setCollectionId(videoInfoVo.getId());
        collectionInfo.setUserId(LoginHelper.getUserId());
        collectionInfo.setCollectionType("0");
        CollectionInfo collection = collectionService.selectCollection(collectionInfo);
        if (ObjectUtil.isNotNull(collection)) {
            videoInfoVo.setIsCollection("0");
        } else {
            videoInfoVo.setIsCollection("1");
        }
        UserLikeBo userLikeBo = new UserLikeBo();
        userLikeBo.setUserId(LoginHelper.getUserId());
        userLikeBo.setLikeTypeId(videoInfoVo.getId());
        userLikeBo.setLikeType("0");
        UserLikeVo userLikeList = userLikeService.queryUserLikeVo(userLikeBo);
        if (ObjectUtil.isNotNull(userLikeList)) {
            videoInfoVo.setIsGiveLike("0");
        } else {
            videoInfoVo.setIsGiveLike("1");
        }
        String orderType = "0";
        if ("1".equals(videoInfoVo.getVideoType())){
            orderType = "1";
        } else if ("2".equals(videoInfoVo.getVideoType())){
            orderType = "4";
        }
        BuyOrder buyOrder = buyOrderService.queryByProductId(videoInfoVo.getId(), null, orderType);
        if (ObjectUtil.isNotNull(buyOrder)) {
            videoInfoVo.setIsBuy("0");
        } else {
            videoInfoVo.setIsBuy("1");
        }

        result.put("videoInfo", videoInfoVo);
        // 猜你喜欢
        VideoInfoBo videoInfoBo = new VideoInfoBo();
        videoInfoBo.setVideoType("0");
        videoInfoBo.setCategoryId(videoInfoVo.getCategoryId());
        List<VideoInfoVo> videoInfoList = videoInfoService.queryList(videoInfoBo);
        result.put("likeVideoList", videoInfoService.getRandomVideos(videoInfoList, 10));
        // 更新观看人数
        bo.setLookNum(videoInfoVo.getLookNum() + 1);
        videoInfoService.updateByBo(bo);
        // 新增播放记录
        // 新增播放记录
        PlayRecordBo playRecordBo = new PlayRecordBo();
        playRecordBo.setVideoId(videoInfoVo.getId());
        playRecordBo.setUserId(LoginHelper.getUserId());
        playRecordBo.setVideoType("0");
        PlayRecord playRecord = playRecordService.queryPlayRecord(playRecordBo);
        if (playRecord == null) {
            playRecordService.insertByBo(playRecordBo);
        }
        return R.ok(result);
    }

    /**
     * 视频分享
     * videoId 视频ID
     */
    @PostMapping("/shareVideoById")
    public R<Map<String, Object>> shareVideoById(@RequestBody JSONObject jsonObject) {
        if (ObjectUtil.isNull(jsonObject.get("videoId"))) {
            return R.fail("视频ID不能为空");
        }
        VideoInfoBo bo = new VideoInfoBo();
        bo.setId(jsonObject.getLong("videoId"));

        Map<String, Object> result = new HashMap<>();
        VideoInfoVo videoInfoVo = videoInfoService.queryById(bo.getId());
        result.put("videoCover", videoInfoVo.getVideoCover());
        result.put("videoName", videoInfoVo.getVideoName());
        result.put("videoLabel", videoInfoVo.getVideoLabel());
        UserInfoVo userInfo = userInfoService.queryById(LoginHelper.getUserId());
        result.put("qrCode", publicService.getQrCode("https://www.baidu.com", userInfo.getInviteCode()));

        bo.setShareNum(videoInfoVo.getShareNum() + 1);
        videoInfoService.updateByBo(bo);

        return R.ok(result);
    }

}
